package com.chujiekeji.test.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RelativeLayout;

import com.chujiekeji.test.adapter.GoodsListAdapter;
import com.chujiekeji.test.base.BaseActivity;
import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.base.BaseConstant;
import com.chujiekeji.test.bean.GoodsBean;
import com.chujiekeji.test.view.PullRefreshView;
import com.chujiekeji.test.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MapleStory
 * @ qq 1002285057
 * @ project https://gitee.com/MapStory/Shopwt-Android
 */

public class ListActivity extends BaseActivity {

    private Toolbar mainToolbar;
    private AppCompatEditText searchEditText;
    private AppCompatImageView toolbarImageView;
    private AppCompatTextView orderTextView;
    private AppCompatTextView saleTextView;
    private AppCompatTextView screenTextView;
    private AppCompatImageView orientationImageView;
    private LinearLayoutCompat orderLinearLayout;
    private RelativeLayout screenRelativeLayout;
    private AppCompatTextView orderCompTextView;
    private AppCompatTextView orderHighTextView;
    private AppCompatTextView orderLowTextView;
    private AppCompatTextView orderHotTextView;
    private AppCompatEditText priceFromEditText;
    private AppCompatEditText priceToEditText;
    private AppCompatTextView confirmTextView;
    private PullRefreshView mainPullRefreshView;

    private String bIdString;
    private String gcIdString;
    private String keywordString;

    private String keyString;
    private String orderString;
    private boolean isGridModel;
    private String priceToString;
    private String priceFromString;

    private int pageInt;
    private GoodsListAdapter mainAdapter;
    private List<GoodsBean> mainArrayList;

    @Override
    public void initView() {

        setContentView(R.layout.activity_goods_list);
        mainToolbar = findViewById(R.id.mainToolbar);
        searchEditText = findViewById(R.id.searchEditText);
        toolbarImageView = findViewById(R.id.toolbarImageView);
        orderTextView = findViewById(R.id.orderTextView);
        saleTextView = findViewById(R.id.saleTextView);
        screenTextView = findViewById(R.id.screenTextView);
        orientationImageView = findViewById(R.id.orientationImageView);
        orderLinearLayout = findViewById(R.id.orderLinearLayout);
        screenRelativeLayout = findViewById(R.id.screenRelativeLayout);
        orderCompTextView = findViewById(R.id.orderCompTextView);
        orderHighTextView = findViewById(R.id.orderHighTextView);
        orderLowTextView = findViewById(R.id.orderLowTextView);
        orderHotTextView = findViewById(R.id.orderHotTextView);
        priceFromEditText = findViewById(R.id.priceFromEditText);
        priceToEditText = findViewById(R.id.priceToEditText);
        confirmTextView = findViewById(R.id.confirmTextView);
        mainPullRefreshView = findViewById(R.id.mainPullRefreshView);

    }

    @Override
    public void initData() {

        bIdString = getIntent().getStringExtra(BaseConstant.DATA_BID);
        gcIdString = getIntent().getStringExtra(BaseConstant.DATA_GCID);
        keywordString = getIntent().getStringExtra(BaseConstant.DATA_KEYWORD);

        setToolbar(mainToolbar, "");
        toolbarImageView.setImageResource(R.drawable.ic_navigation_cate);

        keyString = "";
        orderString = "";
        isGridModel = true;
        priceToString = "";
        priceFromString = "";
        if (!TextUtils.isEmpty(keywordString)) {
            searchEditText.setText(keywordString);
            searchEditText.setSelection(keywordString.length());
        }
        orientationImageView.setImageDrawable(BaseApplication.get().getMipmap(R.mipmap.ic_orientation_grid, R.color.grey));

        pageInt = 1;
        mainArrayList = new ArrayList<>();
        mainAdapter = new GoodsListAdapter(mainArrayList, isGridModel);
        setGirdModel();
        getGoods();

    }

    @Override
    public void initEven() {

        mainPullRefreshView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mainPullRefreshView.isFailure()) {
                    getGoods();
                }
            }
        });

        mainPullRefreshView.setOnRefreshListener(new PullRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageInt = 1;
                getGoods();
            }

            @Override
            public void onLoadMore() {
                getGoods();
            }
        });

    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == Activity.RESULT_OK) {
            switch (req) {
                case BaseConstant.CODE_CLASS:
                    pageInt = 1;
                    mainArrayList.clear();
                    mainAdapter.notifyDataSetChanged();
                    bIdString = intent.getStringExtra(BaseConstant.DATA_BID);
                    gcIdString = intent.getStringExtra(BaseConstant.DATA_GCID);
                    getGoods();
                    break;
                default:
                    break;
            }
        }
    }

    //自定义方法

    private void getGoods() {

        mainPullRefreshView.setLoading();
        if (pageInt == 1) {
            mainArrayList.clear();
        }
        if (pageInt <= 10) {
            JSONObject ggJson = null;
            JSONObject dataJson = null;
            JSONArray itemsJson = null;
            String bean = "{\"code\":200,\"hasmore\":true,\"page_total\":5,\"datas\":{\"goods_list\":[{\"goods_id\":\"30\",\"store_id\":\"1\",\"goods_name\":\"2017\\u590f\\u5b63\\u65b0\\u6b3e\\u4e01\\u5b57\\u6263\\u5e26\\u9c7c\\u5634\\u978b\\u4e32\\u73e0\\u751c\\u7f8e\\u51c9\\u978b\\u5973\\u4e2d\\u8ddf\\u7c97\\u8ddf\\u5973\\u978bL6978\",\"goods_jingle\":\"\",\"goods_price\":\"72.00\",\"goods_marketprice\":\"280.00\",\"goods_image\":\"1_f358cfff9288ea518998844e3fe79e5b.png\",\"goods_salenum\":\"172\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"2\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"\\u5b98\\u65b9\\u81ea\\u8425\",\"is_own_shop\":\"1\",\"sole_flag\":true,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/1\\/1_f358cfff9288ea518998844e3fe79e5b_360.png\"},{\"goods_id\":\"89\",\"store_id\":\"2\",\"goods_name\":\"2017\\u65b0\\u6b3e\\u77ed\\u9774\\u79cb\\u51ac\\u52a0\\u7ed2\\u5973\\u9774\\u97e9\\u7248\\u767e\\u642d\\u9a6c\\u4e01\\u9774\\u6f6e\\u5973\\u978b\\u7c97\\u8ddf\\u9ad8\\u8ddf\\u9774S7008\",\"goods_jingle\":\"\",\"goods_price\":\"181.00\",\"goods_marketprice\":\"181.00\",\"goods_image\":\"2_ceccc355f091db30fad66c822b80dc08.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_ceccc355f091db30fad66c822b80dc08_360.png\"},{\"goods_id\":\"86\",\"store_id\":\"2\",\"goods_name\":\"\\u5973\\u9774\\u65b9\\u5934\\u4e2d\\u8ddf\\u7c97\\u8ddf\\u78e8\\u7802\\u76ae\\u77ed\\u9774\\u65b0\\u6b3e\\u5973\\u978b\\u5b50\\u73cd\\u73e0\\u97e9\\u7248\\u771f\\u76ae\\u77ed\\u7b52\\u9774S7009\",\"goods_jingle\":\"\",\"goods_price\":\"191.00\",\"goods_marketprice\":\"191.00\",\"goods_image\":\"2_6f03a0ec816d5e04e0d53b862bd88fea.png\",\"goods_salenum\":\"2\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_6f03a0ec816d5e04e0d53b862bd88fea_360.png\"},{\"goods_id\":\"85\",\"store_id\":\"2\",\"goods_name\":\"\\u5973\\u978b\\u65b0\\u6b3e\\u79cb\\u51ac\\u6b27\\u7f8e\\u5927\\u724c\\u540c\\u6b3e\\u77ed\\u9774\\u4e2d\\u8ddf\\u88f8\\u9774\\u8857\\u5934\\u673a\\u8f66\\u9774\\u5207\\u5c14\\u897f\\u9774S7002\",\"goods_jingle\":\"\",\"goods_price\":\"199.00\",\"goods_marketprice\":\"199.00\",\"goods_image\":\"2_9d6174b4c93c3509dfc4fcd37078f1ed.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_9d6174b4c93c3509dfc4fcd37078f1ed_360.png\"},{\"goods_id\":\"79\",\"store_id\":\"2\",\"goods_name\":\"\\u6b27\\u7f8e\\u65b0\\u6b3e\\u771f\\u76ae\\u77ed\\u9774\\u5c16\\u5934\\u4e2d\\u8ddf\\u9a6c\\u4e01\\u9774\\u94c6\\u9489\\u5973\\u978b\\u53ca\\u8e1d\\u9774\\u62c9\\u94fe\\u5973\\u9774\\u5b50S7010\",\"goods_jingle\":\"\",\"goods_price\":\"181.00\",\"goods_marketprice\":\"181.00\",\"goods_image\":\"2_906e2ea054cf39fc5fa04796be0e8326.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_906e2ea054cf39fc5fa04796be0e8326_360.png\"},{\"goods_id\":\"78\",\"store_id\":\"2\",\"goods_name\":\"\\u6b27\\u7f8e\\u79cb\\u51ac\\u65b0\\u6b3e\\u957f\\u9774\\u5973\\u5f39\\u529b\\u9774\\u8fc7\\u819d\\u9774\\u5973\\u9774\\u5b50\\u9ad8\\u8ddf\\u5973\\u9774\\u957f\\u7b52\\u9774\\u5973\\u978bS7013\",\"goods_jingle\":\"\",\"goods_price\":\"222.00\",\"goods_marketprice\":\"222.00\",\"goods_image\":\"2_cdd6bf40ba864c4eca7644e6878ef49f.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_cdd6bf40ba864c4eca7644e6878ef49f_360.png\"},{\"goods_id\":\"75\",\"store_id\":\"2\",\"goods_name\":\"\\u771f\\u76ae\\u7c97\\u8ddf\\u77ed\\u9774\\u5973\\u51ac\\u5c16\\u5934\\u94c6\\u9489\\u5973\\u978b\\u5207\\u5c14\\u897f\\u9774\\u5b50\\u82f1\\u4f26\\u98ce\\u9a6c\\u4e01\\u5973\\u9774\\u5b50S7026\",\"goods_jingle\":\"\",\"goods_price\":\"200.00\",\"goods_marketprice\":\"200.00\",\"goods_image\":\"2_b984bda4425e37e5589fe4bb1bd02acc.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_b984bda4425e37e5589fe4bb1bd02acc_360.png\"},{\"goods_id\":\"74\",\"store_id\":\"2\",\"goods_name\":\"\\u771f\\u76ae\\u9ad8\\u8ddf\\u978b\\u7c97\\u8ddf\\u77ed\\u9774\\u5c16\\u5934\\u9a6c\\u4e01\\u9774\\u5973\\u725b\\u76ae\\u79cb\\u51ac\\u5973\\u9774\\u5b50\\u88f8\\u9774\\u5973\\u978b\\u5b50S7019\",\"goods_jingle\":\"\",\"goods_price\":\"176.00\",\"goods_marketprice\":\"176.00\",\"goods_image\":\"2_bc7e2024127d0f49ee19b7f6b263f773.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_bc7e2024127d0f49ee19b7f6b263f773_360.png\"},{\"goods_id\":\"72\",\"store_id\":\"2\",\"goods_name\":\"\\u79cb\\u51ac\\u5f20\\u5929\\u7231\\u540c\\u6b3e\\u5973\\u978b\\u9a6c\\u4e01\\u9774\\u889c\\u9774\\u8fc7\\u819d\\u9774\\u6bdb\\u7ebf\\u9774\\u5f39\\u529b\\u9774\\u5b50\\u9a91\\u58eb\\u9774S7001\",\"goods_jingle\":\"\",\"goods_price\":\"222.00\",\"goods_marketprice\":\"222.00\",\"goods_image\":\"2_9d9789cac32e2689b50417dc54a3f713.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_9d9789cac32e2689b50417dc54a3f713_360.png\"},{\"goods_id\":\"71\",\"store_id\":\"2\",\"goods_name\":\"\\u79cb\\u51ac\\u65b0\\u6b3e\\u5973\\u978b\\u771f\\u76ae\\u7c97\\u9ad8\\u8ddf\\u77ed\\u9774\\u8774\\u8776\\u7ed3\\u9a6c\\u4e01\\u9774\\u78e8\\u7802\\u76ae\\u6f6e\\u65b9\\u5934\\u5973\\u9774S7007\",\"goods_jingle\":\"\",\"goods_price\":\"203.00\",\"goods_marketprice\":\"203.00\",\"goods_image\":\"2_15245d1baf45fde85540a7f69c91cc78.png\",\"goods_salenum\":\"0\",\"evaluation_good_star\":\"5\",\"evaluation_count\":\"0\",\"is_virtual\":\"0\",\"is_presell\":\"0\",\"is_fcode\":\"0\",\"have_gift\":\"0\",\"store_name\":\"ShopWT\\u65d7\\u8230\\u5e97\",\"is_own_shop\":\"0\",\"sole_flag\":false,\"group_flag\":false,\"xianshi_flag\":false,\"goods_image_url\":\"http:\\/\\/demo.shopwt.com\\/system\\/upfiles\\/shop\\/store\\/goods\\/2\\/2_15245d1baf45fde85540a7f69c91cc78_360.png\"}]}}";
            try {
                ggJson = new JSONObject(bean);
                dataJson = ggJson.getJSONObject("datas");
                itemsJson = dataJson.getJSONArray("goods_list");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mainArrayList.addAll((List<GoodsBean>)new Gson().fromJson(itemsJson.toString(), new TypeToken<List<GoodsBean>>(){}.getType()));
            pageInt++;
        }
        mainPullRefreshView.setComplete();

        mainPullRefreshView.setFailure();
    }

    private void setVerModel() {

        mainAdapter = new GoodsListAdapter(mainArrayList, isGridModel);
        mainPullRefreshView.getRecyclerView().setLayoutManager(new LinearLayoutManager(getActivity()));
        mainPullRefreshView.getRecyclerView().setPadding(BaseApplication.get().dipToPx(0), BaseApplication.get().dipToPx(0), BaseApplication.get().dipToPx(0), BaseApplication.get().dipToPx(0));
        mainPullRefreshView.getRecyclerView().setAdapter(mainAdapter);
        mainPullRefreshView.setItemDecoration();
        mainPullRefreshView.setComplete();

        mainAdapter.setOnItemClickListener(new GoodsListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, GoodsBean goodsBean) {
//                BaseApplication.get().startGoods(getActivity(), goodsBean.getGoodsId());
            }

            @Override
            public void onCart(int position, GoodsBean goodsBean) {
//                addCart(goodsBean.getGoodsId());
            }
        });

    }

    private void setGirdModel() {

        mainAdapter = new GoodsListAdapter(mainArrayList, isGridModel);
        mainPullRefreshView.getRecyclerView().setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mainPullRefreshView.getRecyclerView().setPadding(BaseApplication.get().dipToPx(2), BaseApplication.get().dipToPx(2), BaseApplication.get().dipToPx(2), BaseApplication.get().dipToPx(2));
        mainPullRefreshView.getRecyclerView().setAdapter(mainAdapter);
        mainPullRefreshView.clearItemDecoration();
        mainPullRefreshView.setComplete();

        mainAdapter.setOnItemClickListener(new GoodsListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, GoodsBean goodsBean) {
//                BaseApplication.get().startGoods(getActivity(), goodsBean.getGoodsId());
            }

            @Override
            public void onCart(int position, GoodsBean goodsBean) {
//                addCart(goodsBean.getGoodsId());
            }
        });

    }
}
